package com.ruoyi.project.module.shao;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.project.domain.YqgInformationProject;
import com.ruoyi.project.domain.YqgItemAssociation;
import com.ruoyi.project.domain.YqgSetMeal;
import com.ruoyi.project.service.IYqgItemAssociationService;
import com.ruoyi.project.service.IYqgSetMealService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import org.apache.poi.ss.usermodel.DateUtil;
import org.bouncycastle.tsp.TSPUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

import static com.ruoyi.common.security.utils.SecurityUtils.getLoginUser;

/**
 * 套餐信息Controller
 * 
 * @author ruoyi
 * @date 2023-08-15
 */
@RestController
@RequestMapping("/shao/meal")
public class ShaoYqgSetMealController extends BaseController
{
    @Autowired
    private IYqgSetMealService yqgSetMealService;
    @Autowired
    private IYqgItemAssociationService yqgItemAssociationService;

    /**
     * 查询套餐信息列表
     */
//    @RequiresPermissions("project:meal:list")
    @GetMapping("/list")
    public TableDataInfo list(YqgSetMeal yqgSetMeal)
    {
        startPage();
        List<YqgSetMeal> list = yqgSetMealService.selectYqgSetMealList(yqgSetMeal);
        return getDataTable(list);
    }

    /**
     * 导出套餐信息列表
     */
//    @RequiresPermissions("project:meal:export")
    @Log(title = "套餐信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YqgSetMeal yqgSetMeal)
    {
        List<YqgSetMeal> list = yqgSetMealService.selectYqgSetMealList(yqgSetMeal);
        ExcelUtil<YqgSetMeal> util = new ExcelUtil<YqgSetMeal>(YqgSetMeal.class);
        util.exportExcel(response, list, "套餐信息数据");
    }

    /**
     * 获取套餐信息详细信息
     */
//    @RequiresPermissions("project:meal:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(yqgSetMealService.selectYqgSetMealById(id));
    }

    /**
     * 新增套餐信息
     */
//    @RequiresPermissions("project:meal:add")
    @Log(title = "套餐信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YqgSetMeal yqgSetMeal)
    {
        yqgSetMeal.setPackageName(yqgSetMeal.getPackageName());
        List<YqgSetMeal> list = yqgSetMealService.selectYqgSetMealList(yqgSetMeal);
        if(list.size()>0){
            return  new AjaxResult(500,"套餐名不可重复");
        }else {
        //生成患者基本信息表id为水号形式
        Instant now = Instant.now();
        long timestamp = now.getEpochSecond();
        String no = String.valueOf(timestamp);
        //将状态字段赋成01
        yqgSetMeal.setPackageProjectcode(no);
        //0是正常，1是停用
        yqgSetMeal.setPackageStatus("0");
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        yqgSetMeal.setCreatorName(sysUser.getNickName());
        yqgSetMeal.setCreatorTime(DateUtils.getNowDate());
        List<YqgSetMeal> list1 = yqgSetMealService.selectYqgSetMealList(yqgSetMeal);
        return toAjax(yqgSetMealService.insertYqgSetMeal(yqgSetMeal));
        }
    }

    /**
     * 修改套餐信息
     */
//    @RequiresPermissions("project:meal:edit")
    @Log(title = "套餐信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YqgSetMeal yqgSetMeal)
    {
        return toAjax(yqgSetMealService.updateYqgSetMeal(yqgSetMeal));
    }

    /**
     * 删除套餐信息
     */
//    @RequiresPermissions("project:meal:remove")
    @Log(title = "套餐信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        Long[] array = new Long[100];
        YqgSetMeal yqgSetMeal=new YqgSetMeal();
        YqgItemAssociation yqgItemAssociation=new YqgItemAssociation();
        System.out.println(ids[0]);
        yqgSetMeal=yqgSetMealService.selectYqgSetMealById(ids[0]);
        System.out.println(yqgSetMeal.getPackageProjectcode());
        yqgItemAssociation.setSetMealId(Long.valueOf(yqgSetMeal.getPackageProjectcode()));
        List<YqgItemAssociation> list = yqgItemAssociationService.selectYqgItemAssociationList(yqgItemAssociation);
        for(int i=0;i<list.size();i++){
            array[i]= list.get(i).getId();
        }
        System.out.println(array);
        yqgItemAssociationService.deleteYqgItemAssociationByIds(array);
        return toAjax(yqgSetMealService.deleteYqgSetMealByIds(ids));
    }
}
